package com.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * Title:�ַ���빤����
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: flashman.com.cn Copyright (c) 2005
 * </p>
 * <p>
 * Company: flashman.com.cn
 * </p>
 * 
 * @author: jeffzhu
 * @version 1.0
 */
public class CharTools {
	/**
	 * ת������ ISO-8859-1��GB2312
	 * 
	 * @param text
	 * @return
	 */
	public static String ISO2GB(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("ISO-8859-1"), "GB2312");
		} catch (UnsupportedEncodingException ex) {
			result = ex.toString();
		}
		return result;
	}

	/**
	 * ת������ GB2312��ISO-8859-1
	 * 
	 * @param text
	 * @return
	 */
	public static String GB2ISO(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("GB2312"), "ISO-8859-1");
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		return result;
	}

	/**
	 * Utf8URL����
	 * 
	 * @param s
	 * @return
	 */
	public static String Utf8URLencode(String text) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			if (c >= 0 && c <= 255) {
				result.append(c);
			} else {
				byte[] b = new byte[0];
				try {
					b = Character.toString(c).getBytes("UTF-8");
				} catch (Exception ex) {
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					result.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return result.toString();
	}

	/**
	 * Utf8URL����
	 * 
	 * @param text
	 * @return
	 */
	public static String Utf8URLdecode(String text) {
		String result = "";
		int p = 0;
		if (text != null && text.length() > 0) {
			text = text.toLowerCase();
			p = text.indexOf("%e");
			if (p == -1)
				return text;
			while (p != -1) {
				result += text.substring(0, p);
				text = text.substring(p, text.length());
				if (text == "" || text.length() < 9)
					return result;
				result += CodeToWord(text.substring(0, 9));
				text = text.substring(9, text.length());
				p = text.indexOf("%e");
			}
		}
		return result + text;
	}

	/**
	 * utf8URL����ת�ַ�
	 * 
	 * @param text
	 * @return
	 */
	private static String CodeToWord(String text) {
		String result;
		if (Utf8codeCheck(text)) {
			byte[] code = new byte[3];
			code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
			code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
			code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
			try {
				result = new String(code, "UTF-8");
			} catch (UnsupportedEncodingException ex) {
				result = null;
			}
		} else {
			result = text;
		}
		return result;
	}

	/**
	 * �����Ƿ���Ч
	 * 
	 * @param text
	 * @return
	 */
	private static boolean Utf8codeCheck(String text) {
		String sign = "";
		if (text.startsWith("%e"))
			for (int i = 0, p = 0; p != -1; i++) {
				p = text.indexOf("%", p);
				if (p != -1)
					p++;
				sign += p;
			}
		return sign.equals("147-1");
	}

	/**
	 * �Ƿ�Utf8Url����
	 * 
	 * @param text
	 * @return
	 */
	public static boolean isUtf8Url(String text) {
		text = text.toLowerCase();
		int p = text.indexOf("%");
		if (p != -1 && text.length() - p > 9) {
			text = text.substring(p, p + 9);
		}
		return Utf8codeCheck(text);
	}
	/**
	 * char��Ӧ�ı���,���磺&#32993;&#24535;&#26126;����>��־��
	 * 
	 * @param text
	 * @return
	 */
	public static String CharToChinese(String text) {
		String regex;
		final List<String> list = new ArrayList<String>();
		regex = "&#[0-9]+;";
		final Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);
		final Matcher ma = pa.matcher(text);
		while (ma.find()) {
			if(ma.group().length()<10)
				list.add(ma.group());
		}
		char c;
		for(int i=0;i<list.size();i++){
			c=(char)Integer.parseInt(list.get(i).replaceAll("&|#|;", ""));
			//System.out.println(list.get(i));
			text=text.replaceAll(list.get(i)+"",""+c);
		}
		
		return text;
	}
	//ĳ�ַ�ռ���ַ�������
	public static float perCode(String text){
		String title=text.replaceAll(" ", "");
		int sum=countChar(title,'?');
		float p=sum/text.length();
		return p;
	}
	//�ж��ٸ��ַ�
	public static int countChar(String text,char c){
		char t;
		int sum=0;
		for(int i=0;i<text.length();i++){
			t=text.charAt(i);
			if(c==t)sum++;
		}
		return sum;
	}
//	public static boolean isMessyCode(String text){
//		String words=Words.getWords(text);
//		System.out.println(words);
//		return false;
//	}
	/**
	 * ����
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
//		CharTools charTools = new CharTools();
//		String url;
//		url = "���� ����keywords=%E5%86%85%E8%92%99%E5%8F%A4%E5%91%BC%E5%92%8C%E6%B5%A9%E7%89%B9";
//		if (charTools.isUtf8Url(url)) {
//			System.out.println(charTools.Utf8URLdecode(url));
//		} else {
//			System.out.println(charTools.CodeToWord(url));
//		}
		System.out.println(CharTools.CharToChinese("Title:&#32993;&#24535;&#26126;&#24066;&#37202;&#24215;, "));
	//	int c='?';
		int t='1';
		int c='9';
		System.out.println(t+":"+c);
	}
}